<?php
    use User\Model\MeetingModel;
?>
<ul class="nav nav-tabs nav-justified">
    <li><a href="<?php echo '/User/Meeting/lucky_index?meetId=' . I('get.meetId', '', '');?>" onmouseenter="$(this).css('color', '#FFFFFF');" onmouseleave="$(this).css('color', '#65CEA7');">基础设置</a></li>
    <li class="active"><a href="#" onmouseenter="$(this).css('color', '#FFFFFF');" onmouseleave="$(this).css('color', '#65CEA7');">奖项设置</a></li>
    <li><a href="<?php echo '/User/Meeting/lucky_user?meetId=' . I('get.meetId', '', '');?>" onmouseenter="$(this).css('color', '#FFFFFF');" onmouseleave="$(this).css('color', '#65CEA7');">中奖名单</a></li>
</ul>
<div class="row">
    <div class="col-lg-12">
        <section class="panel">
            <header class="panel-heading" >
                <button class="btn btn-success" onclick="show_modal();">新增</button>
            </header>
            <div class="panel-body">
                <table class="table table-hover general-table table-bordered">
                    <thead>
                    <tr>
                        <th width="15%">奖项等级</th>
                        <th width="15%">奖项名称</th>
                        <th width="10%">奖项数量</th>
                        <th width="30%">奖项图片</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach($list as $val){?>
                    <tr>
                        <td>
                            <span>
                                <?php echo $val['name'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo $val['product_name'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <?php echo $val['num'];?>
                            </span>
                        </td>
                        <td>
                            <span>
                                <img src="<?php echo $val['pic'];?>" alt="" style="width: 200px;">
                            </span>
                        </td>
                        <td>
                            <div>
                                <button class="btn btn-default" onclick="show_modal(this)" data-item-id="<?php echo $val['item_id'];?>" data-name="<?php echo $val['name'];?>"
                                data-product-name="<?php echo $val['product_name'];?>" data-num="<?php echo $val['num'];?>" data-pic="<?php echo $val['pic'];?>">编辑</button>
                                <a class="btn btn-danger" href="#"
                                   onclick="<?php echo 'return delete_data(' . $val['item_id'] .')';?>">删除</a>
                            </div>
                        </td>
                    </tr>
                    <?php }?>

                    </tbody>
                </table>
                <div class="pages">
                    {$page}
                </div>
            </div>
        </section>
    </div>
</div>
<div class="modal fade myModal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">奖项设置</h4>
            </div>
            <div class="modal-body">
                <form>
                    <input type="hidden" name="meetId" value="<?php echo I('get.meetId', '', '');?>" id="meetId">
                    <input type="hidden" name="item_id" value="" id="item_id">
                    <div>奖项名称：<input class="form-control" type="text" id="name" name="name"></div>
                    <div>奖品名称：<input class="form-control" type="text" id="product_name" name="product_name"></div>
                    <div>奖品数量：<input class="form-control" type="text" id="num" name="num"></div>
                    <div>奖品图片：<img src="" alt="" id="pic" style="width: 100px;height: 100px;"><input type="file" id="upload_img"></div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal" onclick="form_submit();">提交</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div>
</div>
<script>
    $(".need").each(function(){
        $(this).prepend("<span style='color:red'>*</span>");
    });
    function delete_data(id){
        $.ajax({
            data:{item_id:id},
            dataType:'json',
            url:'/User/Meeting/lucky_item_delete',
            type:'post',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
    function show_modal(obj){
        var item_id = $(obj).attr("data-item-id");
        var name = $(obj).attr("data-name");
        var product_name = $(obj).attr("data-product-name");
        var num = $(obj).attr("data-num");
        var pic = $(obj).attr("data-pic");
        $("#item_id").val(item_id);
        $("#name").val(name);
        $("#product_name").val(product_name);
        $("#num").val(num);
        $("#pic").attr("src", pic);
        $("#myModal").modal("show");
    }
    $("#myModal").on('hidden.bs.modal', function(){
        picurl_data = '';
        $("#item_id").val('');
        $("#name").val('');
        $("#product_name").val('');
        $("#num").val('');
        $("#pic").attr("src", "");
        $("#upload_img").val('');
    });
    var picurl_data = '';
    $(function(){
        var img_upload = document.getElementById("upload_img");
        img_upload.addEventListener('change',readFile,false);
    });
    function readFile(){
        var file = this.files[0];
        if(!/image\/\w+/.test(file.type)){
            alert("请确保文件为图像类型");
            return false;
        }
        var reader=new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function() {
            $("#pic").attr("src", this.result);
            picurl_data = this.result;
        }
    }
    function form_submit(){
        $.ajax({
            type: 'post',
            url: '/User/Meeting/lucky_item_edit',
            data:{
                meetId: $("#meetId").val(),
                item_id: $("#item_id").val(),
                name: $("#name").val(),
                product_name: $("#product_name").val(),
                num: $("#num").val(),
                pic: picurl_data
            },
            dataType:'json',
            success:function(res){
                window.location.reload();
            }
        });
        return false;
    }
</script>